package com.example.myapplication.base;

import android.app.Application;

import com.example.myapplication.api.Constants;
import com.example.myapplication.app.App;
import com.example.myapplication.model.rx.RxUtil;
import com.example.myapplication.utils.ScreenUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;


/**
 * Author:caiyoufei
 * Date:2016/11/12
 * Time:20:38
 */
public class RxPresenter<T extends BaseView> implements BasePresenter<T> {

    protected T mView;
    //应用权限需要使用
    protected Application mApplication;
    //屏幕宽高
    protected int mScreenWidth;
    protected int mScreenHeight;
    protected int mStatusBarHeight;

    protected CompositeDisposable mCompositeDisposable;

    protected void unDisposable() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.dispose();
        }
    }

    protected void addDisposable(Disposable subscription) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(subscription);
    }
    protected void addDisposableTime(Disposable subscription) {
        Disposable disposableCountDown = Flowable.timer(Constants.MAX_REQUEST_TIME, TimeUnit.MILLISECONDS)
                .compose(RxUtil.rxSchedulerHelper()).subscribe(aLong -> {
                    subscription.dispose();
                });
        addDisposable(disposableCountDown);
    }

    @Override
    public void attachView(T view) {
        this.mView = view;
        this.mApplication = App.getInstance();
        this.mScreenWidth = ScreenUtils.getScreenWidth(mApplication);
        this.mScreenHeight = ScreenUtils.getScreenHeight(mApplication);
        this.mStatusBarHeight = ScreenUtils.getStatusHeight(mApplication);
    }

    @Override
    public void detachView() {
        this.mView = null;
        unDisposable();
    }
}
